package com.autotest.nore.common.utils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

@Component
public class HeaderParserUtils {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static HashMap<String, String> parseHeaders(String processedHeader, HashMap<String, String> requestHeaders) {
        try {
            JsonNode rootNode = objectMapper.readTree(processedHeader);
            if (rootNode.isArray()) {
                Iterator<JsonNode> elements = rootNode.elements();
                while (elements.hasNext()) {
                    JsonNode element = elements.next();
                    String name = element.get("name").asText();
                    String value = element.get("value").asText();
                    requestHeaders.put(name, value);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return requestHeaders;
    }
}
